using cap01.Web.Core.Extensions;
using Microsoft.AspNetCore.ResponseCompression;
using Serilog;

namespace cap01.Web.Core;

public class Startup
{
	public void ConfigBuilder(WebApplicationBuilder builder)
	{
        var services = builder.Services;

        // 加入系统配置文件
        builder.Configuration.AddConfiguration();

        // 提供IHttpContextAccessor对象
        services.AddHttpContextAccessor();

		services.AddResponseCompression(options => { options.Providers.Add<GzipCompressionProvider>(); });

        // 提供openapi文档说明
        services.AddSwaggerDoc();

        // 用户认证
        services.AddHuaskyAuth();

        // 根据程序集反射出需要注入的服务
        services.AddHuaskyApiServices();

        // 根据程序集反射出需要后台运行的任务
        services.AddHuaskyApiTasks();

        // 提供Redis缓存
        services.AddRedisCache();

        // 提供控制器服务
        services.AddHuaskyController();

        // 提供控制器服务
        services.AddHuaskyMapper();

        // CAP
        services.AddHuaskyCap();

        builder.Host.UseSerilog();
        builder.Host.UseConsoleLifetime();
    }

	public void ConfigApp(WebApplication app)
	{
		if (!app.Environment.IsDevelopment())
		{
			app.UseExceptionHandler();
		}

        app.UseResponseCompression();

        app.UseSerilogRequestLogging();

        app.UseHuaskyAuth();

        app.UseSwaggerDoc();

        app.MapControllers();

    }


    public static WebApplication Build(Startup startup)
    {
        var builder = WebApplication.CreateBuilder();
        startup.ConfigBuilder(builder);

        var app = builder.Build();
        startup.ConfigApp(app);

        //app.Urls.Add($"http://+:{AppConfig.Settings.Port}");

        return app;
    }
}